<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/9
 * Time: 17:47
 */

namespace app\admin\controller;


use think\Db;

class Shop extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $start_time = input('start_time');
            $end_time = input('end_time');
            $user_keyword = input('user_keyword');
            $shop_keyword = input('shop_keyword');
            $user_status = input('user_status');
            
            $where = [];
            if(isset($user_keyword) && $user_keyword != ''){
                $where['user_id|user_username|user_phone'] = ['like', '%' . $user_keyword . '%'];
            }
            if(isset($shop_keyword) && $shop_keyword != ''){
                $where['shop_id|shop_name'] = ['like', '%' . $shop_keyword . '%'];
            }
            if(isset($user_status) && $user_status != ''){
                $where['user_status'] = $user_status;
            }
            // 时间
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['user_painter_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['user_painter_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['user_painter_time'] = ['<', $end_time];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('shop')
                ->join('up_u_user', 'user_id = shop_user_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('shop')
                ->join('up_u_user', 'user_id = shop_user_id', 'LEFT')
                ->field('shop_id, shop_name, shop_logo, shop_goods_type, user_username, user_id, user_phone, 
                user_email, user_status, user_role, user_painter_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('user_painter_time desc')
                ->select();
            if($lists){
                foreach($lists as $k => $v){
                    // 主营商品
                    $goods_type = Db::table('up_goods_type')->where('gt_id', 'IN', $v['shop_goods_type'])->column('gt_name');
                    $lists[$k]['shop_goods_type'] = implode(',', $goods_type);
                    // --------------------- 店铺总流水、店铺当日流水、店铺总销量、店铺当日销量
                    $today_begin_time = date('Y-m-d 00:00:01');
                    $today_end_time = date('Y-m-d 23:23:59');
                    $total = Db::table('up_goods_order')
                        ->where('order_sail_user_id', $v['user_id'])
                        ->where('order_status', 'IN', '2,3,4,7,9')
                        ->where('order_type', 'IN', '1,2,3,4')
                        ->field('order_pay_price, order_num')
                        ->select();
                    $today = Db::table('up_goods_order')
                        ->where('order_sail_user_id', $v['user_id'])
                        ->where('order_status', 'IN', '2,3,4,7,9')
                        ->where('order_type', 'IN', '1,2,3,4')
                        ->where('order_create_time', 'between', [$today_begin_time, $today_end_time])
                        ->field('order_pay_price, order_num')
                        ->select();
                    $total_price = 0; // 店铺总流水
                    $total_num = 0; // 店铺总销量
                    $today_price = 0; // 店铺当日流水
                    $today_num = 0; // 店铺当日销量
                    if($total){
                        foreach($total as $kk => $vv){
                            $total_price += $vv['order_pay_price'];
                            $total_num += $vv['order_num'];
                        }
                    }
                    if($today){
                        foreach($today as $ke => $vo){
                            $today_price += $vo['order_pay_price'];
                            $today_num += $vo['order_num'];
                        }
                    }
                    $lists[$k]['total_price'] = $total_price;
                    $lists[$k]['total_num'] = $total_num;
                    $lists[$k]['today_price'] = $today_price;
                    $lists[$k]['today_num'] = $today_num;
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 店铺简介
     * @return mixed
     */
    public function shopDetail(){
        $shop_id = input('shop_id');
        $shop = Db::table('up_shop')
            ->where('shop_id', $shop_id)
            ->field('shop_intro')
            ->find();
        $this->assign('shop', $shop);
        return $this->fetch('shop/shopdetail');
    }
}